package com.hc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hc.domain.DictType;
import com.hc.dto.ListDictTypeDTO;
import com.hc.vo.DictTypeVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 字典类型
 *
 * @author 梁云亮
 */
@Mapper
public interface DictTypeMapper extends BaseMapper<DictType> {
    /**
     * 添加字典类型
     *
     * @param dictTypeVO
     * @return
     */
    @Insert("INSERT INTO tb_dict_type (name, status, parent_id, create_time) VALUES (#{dictTypeVO.name} ,#{dictTypeVO.status} ,#{dictTypeVO.parentId} ,now())")
    int insertDictType(@Param("dictTypeVO") DictTypeVO dictTypeVO);

    /**
     * 更新字典类型
     *
     * @param id
     * @param dictTypeVO
     * @return
     */
    int updateDictTypeSelective(@Param("id") Integer id, @Param("dictTypeVO") DictTypeVO dictTypeVO);

    /**
     * 根据id删除字典类型
     *
     * @param id 字典类型编号
     * @return
     */
    @Delete("DELETE FROM tb_dict_type WHERE id = #{id}")
    int deleteDictTypeById(@Param("id") Integer id);

    /**
     * 根据名称查询字典类型
     *
     * @param name
     * @return
     */
    @Select("SELECT count(*) FROM tb_dict_type WHERE name =#{name}")
    int selectDictTypeByName(@Param("name") String name);

    /**
     * 根据父类别查询所有的字典类型
     *
     * @param name
     * @return
     */
    @Select("SELECT dt1.id, dt1.`name` \n" +
            "FROM tb_dict_type dt1\n" +
            "INNER JOIN tb_dict_type dt2 ON dt1.parent_id = dt2.id \n" +
            "WHERE	dt2.name = #{name} ")
    List<ListDictTypeDTO> selectDictTypeByParentName(@Param("name") String name);
}
